home *** CD-ROM | disk | FTP | other *** search
- Path: news.spies.com!usenet
- From: Erik Max Francis <max@alcyone.com>
- Newsgroups: comp.lang.c
- Subject: Re: Help: array of ptrs to structures
- Date: Wed, 03 Apr 1996 21:13:33 -0800
- Organization: Alcyone Systems
- Message-ID: <31635A7D.408405C4@alcyone.com>
- References: <Pine.SOL.3.91-941213.960403155531.22205C-100000@altair.dur.ac.uk>
- NNTP-Posting-Host: newton.alcyone.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
-
- Ed Wiles wrote:
- >
- > [I'm a bit of a C novice, would appreciate your (combined) help, thanks!]
- >
- > "struct person" contains three variables: lastname, firstname, phone
- > (all arrays of char).
- >
- > Within main(), I've declared an array of pointers to "struct person":
- > struct person *phone_book[6];
- >
- > 1) How do I hard-code, for example, the third person in the array to be:
- > lastname = "Adcock"; firstname = "Lucy"; phone = "(01623) 943086"; ?
- >
- > *phone_book[2] = {"Adcock", "Lucy", etc.} is not allowed, so do I have to
- > assign each structure member individually?
- > i.e. *phone_book[2]->lastname = "Adcock", etc.
-
- You have to use the string functions in C. Strings are not fundamental
- datatypes in C; strings are merely arrays of characters which follow the
- convention of being NUL-terminated.
-
- > (In fact, I really want to hard-code all the people in the array in one
- > go: is there a way of doing this when I declare the variable phone_book,
- > remembering that e.g. int odds[] = {1, 3, 5, 7} is allowed?)
-
- You can do this with an initialization when you declare the array (but not if
- you declare an array of pointers, at least not easily in a way that will be
- useful to you).
-
- It will look something like this:
-
- struct person phoneBook[] = { { "Adcock", "Lucy", "(01623) 943086" },
- { "Doe", "John", "415 555 1212" },
- /* and so on */ };
-
- > Also, the pointer phone_book[2] is uninitialised, so it could be the
- > address of anywhere... How do I initialise a pointer, please? (It is
- > NEW(pointer_name) in Modula-2, so there must be a C equivalent..?)
-
- That's the essential point here. Look into malloc and free.
-
- Note that if you are merely interested in a fixed array of six structures,
- using dynamic allocation is probably a bad idea (unless you have a good reason
- for doing this). Just declare them statically.
-
- > 2) I want to pass the entire array to a function. Do I just pass it as
- > phone_book? Should the function's corresponding formal parameter be
- > struct person *anyname[] ?
- >
- > Thanks a lot!
- >
- > - Ed Wiles (e.d.wiles@dur.ac.uk)
-
- --
- Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
- San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
- H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
- Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
- "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
-